package com.ruoyi.member.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.member.domain.MemberWallet;
import com.ruoyi.member.service.IMemberWalletService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员钱包Controller
 *
 * @author ruoyi
 * @date 2025-01-22
 */
@RestController
@RequestMapping("/member/wallet")
public class MemberWalletController extends BaseController {
    @Autowired
    private IMemberWalletService memberWalletService;

    /**
     * 查询会员钱包列表
     */
    @PreAuthorize("@ss.hasPermi('member:wallet:list')")
    @GetMapping("/list")
    public TableDataInfo list(MemberWallet memberWallet) {
        startPage();
        List<MemberWallet> list = memberWalletService.selectMemberWalletList(memberWallet);
        return getDataTable(list);
    }

    /**
     * 导出会员钱包列表
     */
    @PreAuthorize("@ss.hasPermi('member:wallet:export')")
    @Log(title = "会员钱包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberWallet memberWallet) {
        List<MemberWallet> list = memberWalletService.selectMemberWalletList(memberWallet);
        ExcelUtil<MemberWallet> util = new ExcelUtil<MemberWallet>(MemberWallet.class);
        util.exportExcel(response, list, "会员钱包数据");
    }

    /**
     * 获取会员钱包详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:wallet:query')")
    @GetMapping(value = "/{walletId}")
    public AjaxResult getInfo(@PathVariable("walletId") Long walletId) {
        return AjaxResult.success(memberWalletService.selectMemberWalletById(walletId));
    }

    /**
     * 新增会员钱包
     */
    @PreAuthorize("@ss.hasPermi('member:wallet:add')")
    @Log(title = "会员钱包", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberWallet memberWallet) {
        return toAjax(memberWalletService.insertMemberWallet(memberWallet));
    }

    /**
     * 修改会员钱包
     */
    @PreAuthorize("@ss.hasPermi('member:wallet:edit')")
    @Log(title = "会员钱包", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberWallet memberWallet) {
        return toAjax(memberWalletService.updateMemberWallet(memberWallet));
    }

    /**
     * 删除会员钱包
     */
    @PreAuthorize("@ss.hasPermi('member:wallet:remove')")
    @Log(title = "会员钱包", businessType = BusinessType.DELETE)
    @DeleteMapping("/{walletIds}")
    public AjaxResult remove(@PathVariable Long[] walletIds) {
        return toAjax(memberWalletService.deleteMemberWalletByIds(walletIds));
    }

    /**
     * 充值
     */
    @PreAuthorize("@ss.hasPermi('member:wallet:recharge')")
    @Log(title = "会员充值", businessType = BusinessType.UPDATE)
    @PostMapping("/recharge")
    public AjaxResult recharge(@RequestBody MemberWallet memberWallet) {
        // TODO: 需要修改为使用Integer类型的gold字段
        return error("充值功能需要重构为使用金币(Integer)");
    }

    /**
     * 扣费
     */
    @PreAuthorize("@ss.hasPermi('member:wallet:deduct')")
    @Log(title = "会员扣费", businessType = BusinessType.UPDATE)
    @PostMapping("/deduct")
    public AjaxResult deduct(@RequestBody MemberWallet memberWallet) {
        // TODO: 需要修改为使用Integer类型的gold字段
        return error("扣费功能需要重构为使用金币(Integer)");
    }
}